/**
 * @file merge_sorted_list.cpp
 * @author mixinju@outlook.com
 * @brief 合并两个有序链表
 * @version 0.1
 * @date 2021-11-20
 *
 * @copyright Copyright (c) 2021
 *
 */

//题目来源:https://leetcode-cn.com/problems/merge-two-sorted-lists/

#include "list.h"
ListNode* mergeTwoLists(ListNode* first, ListNode* second) {
  ListNode* dummy = new ListNode();  //使用哑结点减少过多的逻辑处理语句
  ListNode* pre = dummy;

  while (first && second) {
    if (first->data_ <= second->data_) {
      pre->next_ = first;
      first = first->next_;
    } else {
      pre->next_ = second;
      second = second->next_;
    }
    pre = pre->next_;
  }

  pre->next_ = (first) ? first : second;

  pre = dummy->next_;  //释放结点
  delete dummy;
  return pre;
}